package com.lky.marshallmall.order.interceptor;

import com.lky.common.constant.AuthServerConstant;
import com.lky.common.vo.MemberVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by
 * autor : Marshall lky
 * datetime: 2021/6/7 20:03
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {

        String requestURI = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/status/**", requestURI);
        if (match){
            return true;
        }

        HttpSession session = request.getSession();
        MemberVo attribute = (MemberVo) session.getAttribute(AuthServerConstant.SESSION_NAME);
        if (attribute!=null){
            loginUser.set(attribute);
            return true;
        }else {
            //没登陆去登录
            request.getSession().setAttribute("msg","请先登录");
            response.sendRedirect("http://auth.marshallmall.com/login.html");
            return false;
        }
    }
}
